//index.js
import store from '../../store/index'
import { getIndexDatePatInfo } from '../../api/publishDatePat'
import { error } from '../../lib/js/utils'
import { IMAGE_SERVER_URL } from '../../api/config'

// 创建页面
Page(
  store.createPage({
    data: {
      datePatInfo: [],
      currentPage: 0
    },

    /**
     * 初始化数据
     */
    initData: function () {
      this.getNextDatePatInfo()
    },
    /**
     * 加载下一页数据
     */
    getNextDatePatInfo: function () {
      const that = this
      const currentPage = that.data.currentPage
      const nextPage = currentPage + 1
      const datePatInfo = that.data.datePatInfo
      getIndexDatePatInfo(nextPage).then(function (res) {
        if (res.data.rows.length === 0) {
          error('没有数据了')
          return
        }
        res.data.rows.forEach(item => {
          if (item.userVo.avatar.url.indexOf('http') === -1) {
            item.userVo.avatar.url = `${IMAGE_SERVER_URL}/${item.userVo.avatar.url}`
          }
          for (var i = 0; i < item.images.length; i++) {
            item.images[i].url = `${IMAGE_SERVER_URL}/${item.images[i].url}`
          }
          datePatInfo.push(item)
        })
        that.setData({
          currentPage: nextPage,
          datePatInfo: datePatInfo
        })
      })
    },
    /**
     * 跳转到约拍信息详细页面
     */
    navigateToDatePatDetail: function (e) {
      const that = this
      const index = e.currentTarget.dataset.index
      const datePatInfoStr = JSON.stringify(that.data.datePatInfo[index])
      wx.navigateTo({
        url: `/pages/date_pat_detail/date_pat_detail?datePatInfoStr=${datePatInfoStr}`
      })
    },
    onLoad: function () {
      this.initData()
    },
    onReachBottom: function () {
      this.getNextDatePatInfo()
    },

    //求数组的长度
    getJsonlength: function(jsonData){
      var jasonLength = 0;
      for(var item in jsonData){
        jasonLength++;
      }
      return jasonLength;
    },

    

    /**
     * 下拉刷新页面用户数据
     */
    onPullDownRefresh: function () {
      const that = this
      const datePatInfo = that.data.datePatInfo
      const thisPage = that.data.currentPage
      const timeLength = that.getJsonlength(that.data.datePatInfo)
      //console.log(timeLength)
      //console.log(this.data.currentPage)
      const pushPoint = timeLength%5
      var count = 1
      
      //console.log(pushPoint)
      if((timeLength%5)!=0){
        //此时的长度不是5的整数倍的时候，依旧获取该页面的数据
        getIndexDatePatInfo(thisPage).then(function (res){
          if (res.data.rows.length === 0){
            error('没有数据了')
            return
          }
          console.log(res.data.rows)
          res.data.rows.forEach(item => {
            //这个地方留了一个问题，因为这里采用的是forEach，我的想法是
            //从pushPoint的位置开始向数组中增加元素，避免出现重复的元素
            //还有一种办法，遍历数组，进行比较，如果有重复的地方，选择不添加
            // console.log(item.id)
            // console.log(pushPoint)                       
            if(count > pushPoint){           
              if (item.userVo.avatar.url.indexOf('http') === -1) {
                item.userVo.avatar.url = `${IMAGE_SERVER_URL}/${item.userVo.avatar.url}`
              }
              for (var i = 0; i < item.images.length; i++) {
                item.images[i].url = `${IMAGE_SERVER_URL}/${item.images[i].url}`
              }
              datePatInfo.push(item)
            }
            count += 1
            //console.log(count)
          })
          that.setData({
            datePatInfo: datePatInfo
          })
        })
      }
    }
  })
)

